package yadong.example.code;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;


public class TextHandler {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public static void main(String[] args) throws ClientProtocolException, IOException {
		Scanner s = new Scanner(new File("src/main/resources/txtFile.txt"));
		String line = null;
		String line1 = null;
		String prefix = "<a href=\"http://glassadmin.digitalink.com/eidos-search-notifier/notify?operation=delete&app=GOG&url=http://glassadmin.digitalink.com/gogdocs/id/";
		String postfix = ".xml\">1</a></br>\"";
		List<String> urlList = new ArrayList<>();
		int count = 1;
		while (s.hasNextLine()){
			line = s.nextLine();
			if (line!=null&&line.startsWith("<string>egcontentid")){
				line1 = s.nextLine();
				urlList.add("http://glassadmin.digitalink.com/eidos-search-notifier/notify?operation=delete&app=GOG&url=http://glassadmin.digitalink.com/gogdocs/id/"+StringUtils.substringBetween(line1, "<string>", "</string>")+".xml");
//				System.out.println(prefix+StringUtils.substringBetween(line1, "<string>", "</string>")+".xml\"> click on "+(count++)+"</a></br>");
			}
		}
		HttpClient client = new DefaultHttpClient();
		HttpGet request = null;
		HttpResponse response = null;
		BufferedReader rd  = null;
		for (String url : urlList) {
			System.out.println(url);
			request = new HttpGet(url);
			response = client.execute(request);
			// Get the response
			rd = new BufferedReader
					(new InputStreamReader(response.getEntity().getContent()));		
			String rs = "";
			while ((rs = rd.readLine()) != null) {
				System.out.println("response:"+rs);
			} 
		}
	}

}
